//
// * Copyright (c) 2002-2009 "Neo Technology,"
// *     Network Engine for Objects in Lund AB [http://neotechnology.com]
// *
// * This file is part of Neo4j.
// * 
// * Neo4j is free software: you can redistribute it and/or modify
// * it under the terms of the GNU Affero General Public License as
// * published by the Free Software Foundation, either version 3 of the
// * License, or (at your option) any later version.
// * 
// * This program is distributed in the hope that it will be useful,
// * but WITHOUT ANY WARRANTY; without even the implied warranty of
// * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// * GNU Affero General Public License for more details.
// * 
// * You should have received a copy of the GNU Affero General Public License
// * along with this program. If not, see <http://www.gnu.org/licenses/>.
// 
namespace org.neo4j.kernel.impl.core
{

	using Node = org.neo4j.graphdb.Node;
	using Relationship = org.neo4j.graphdb.Relationship;
	using RelationshipType = org.neo4j.graphdb.RelationshipType;

	internal class RelationshipProxy : Relationship
	{
		private readonly int relId;
		private readonly NodeManager nm;

		internal RelationshipProxy(int relId, NodeManager nodeManager)
		{
			this.relId = relId;
			this.nm = nodeManager;
		}

		public virtual long getId()
		{
			return relId;
		}

		public virtual void delete()
		{
			nm.getRelForProxy(relId).delete();
		}

		public virtual Node[] getNodes()
		{
			return nm.getRelForProxy(relId).getNodes();
		}

		public virtual Node getOtherNode(Node node)
		{
			return nm.getRelForProxy(relId).getOtherNode(node);
		}

		public virtual Node getStartNode()
		{
			return nm.getRelForProxy(relId).getStartNode();
		}

		public virtual Node getEndNode()
		{
			return nm.getRelForProxy(relId).getEndNode();
		}

		public virtual RelationshipType getType()
		{
			return nm.getRelForProxy(relId).getType();
		}

		public virtual IEnumerable <string> getPropertyKeys()
		{
			return nm.getRelForProxy(relId).getPropertyKeys();
		}

		public virtual IEnumerable <object> getPropertyValues()
		{
			return nm.getRelForProxy(relId).getPropertyValues();
		}

		public virtual object getProperty(string key)
		{
			return nm.getRelForProxy(relId).getProperty(key);
		}

		public virtual object getProperty(string key, object defaultValue)
		{
			return nm.getRelForProxy(relId).getProperty(key, defaultValue);
		}

		public virtual bool hasProperty(string key)
		{
			return nm.getRelForProxy(relId).hasProperty(key);
		}

		public virtual void setProperty(string key, object property)
		{
			nm.getRelForProxy(relId).setProperty(key, property);
		}

		public virtual object removeProperty(string key)
		{
			return nm.getRelForProxy(relId).removeProperty(key);
		}

		public virtual bool isType(RelationshipType type)
		{
			return nm.getRelForProxy(relId).isType(type);
		}

		public virtual int compareTo(object rel)
		{
			Relationship r = (Relationship) rel;
			int ourId = (int) this.getId(), theirId = (int) r.getId();

			if (ourId < theirId)
			{
				return -1;
			}
			else if (ourId > theirId)
			{
				return 1;
			}
			else
			{
				return 0;
			}
		}

		public override bool Equals(object o)
		{
			if (!(o is Relationship))
			{
				return false;
			}
			return this.getId() == ((Relationship) o).getId();

		}

		public override int GetHashCode()
		{
			return relId;
		}

		public override string ToString()
		{
			return "Relationship[" + this.getId() + "]";
		}
	}
}